package com.delivery.www.dao.DaoImpl;

import com.delivery.www.dao.BanDao;
import com.delivery.www.dao.BaseDao;
import com.delivery.www.po.Ban;
import com.delivery.www.util.JdbcUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @author 12970
 */
public class BanDaoImpl implements BanDao {
    @Override
    public int doBan(int userId) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "insert into ban (user_id) values(?)";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, userId);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public List<Ban> getBan() {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select ban_id,user_id from ban";
        List<Ban> bans = null;
        if (connection != null) {
            bans = BaseDao.getList(connection, Ban.class, sql);
            JdbcUtils.closeResource(connection, null, null);
        }
        return bans;
    }

    @Override
    public int cancelBan(int userId) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "delete from ban where user_id = ?";
        int i = 0;
        if (connection != null) {
            i = BaseDao.executeUpdate(connection, sql, userId);
            JdbcUtils.closeResource(connection, null, null);
        }
        return i;
    }

    @Override
    public Ban get(int userId) {
        Connection connection = JdbcUtils.getConnection();
        String sql = "select ban_id,user_id from ban where user_id = ?";
        Ban ban = null;
        Object[] params = {userId};
        if (connection != null) {
            ban = BaseDao.getForOne(connection, Ban.class, sql, params);
            JdbcUtils.closeResource(connection, null, null);
        }
        return ban;
    }
}
